package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.CustomerQueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;





    @RequiresPermissions(value = {"customer:poolList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 只显示状态是潜在的客户
        model.addAttribute("sellers",employeeService.selectByRoleSn("Market","Market_Manager"));
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("jobs",systemDictionaryItemService.selectByDicSn("job"));
        model.addAttribute("sources",systemDictionaryItemService.selectByDicSn("source"));
        model.addAttribute("ccts",systemDictionaryItemService.selectByDicSn("communicationMethod"));
        return "customer/poolList";
    }

    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 查找销售
        model.addAttribute("sellers",employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"}));
        // 查询所有状态的
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        // 共享所有的职位
        model.addAttribute("jobs",systemDictionaryItemService.selectByDicSn("job"));
        // 共享所有的来源
        model.addAttribute("sources",systemDictionaryItemService.selectByDicSn("source"));
        return "customer/list";
    }


    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        Subject subject = SecurityUtils.getSubject();
        // 不是超管或市场经理,把id 设置到qo,只显示该销售的客户
        if(!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))){
            Employee employee = UserContext.getCurrentUser();
            qo.setSellerId(employee.getId());
        }
        // 查找销售
        // 只显示状态是潜在的客户
         model.addAttribute("sellers",employeeService.selectByRoleSn("Market","Market_Manager"));
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("jobs",systemDictionaryItemService.selectByDicSn("job"));
        model.addAttribute("sources",systemDictionaryItemService.selectByDicSn("source"));
        model.addAttribute("ccts",systemDictionaryItemService.selectByDicSn("communicationMethod"));
        return "customer/potentialList";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if(customer.getGender() < 0){
            return new JsonResult(false,"请选择性别");
        }
        if(customer.getJob().getId() < 0){
            return new JsonResult(false,"请选择工作");
        }
        if(customer.getSource().getId() < 0){
            return new JsonResult(false,"请选择来源");
        }
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
